package com.hxjy.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 处理SESSION权限
 * @author 李彬
 *
 */
public class SessionFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest hrequest = (HttpServletRequest) request;
		HttpServletResponse hresponse = (HttpServletResponse) response;
		//HttpServletResponse hresponse = (HttpServletResponse) response;
		HttpSession session = hrequest.getSession(false);
		if(!hrequest.isRequestedSessionIdValid()){
			String msg = "session失效";
			//System.out.println(msg);
			request.setAttribute("login_error_msg", msg);
            hrequest.getRequestDispatcher("/autoForwardLogin.jsp").forward(request, response);

		}else if(session.getAttribute("loginUser") != null){
			//System.out.println("session有效");
            chain.doFilter(request, response); 
		}else{
			String msg = "session失效";
			//System.out.println(msg);
			request.setAttribute("login_error_msg", msg);
			hrequest.getRequestDispatcher("/autoForwardLogin.jsp").forward(request, response);
		}
		
		
	}


	public void init(FilterConfig config) throws ServletException {

	}

}
